package controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

import db.DBConnection;
import db.DBItem;
import db.DBItemSupplier;
import db.DBItemType;
import db.DBItemTypeMeasurement;
import db.IFItem;
import db.IFItemSupplier;
import db.IFItemType;
import db.IFItemTypeMeasurement;
import model.Item;
import model.ItemSupplier;
import model.ItemType;
import model.ItemTypeMeasurement;
import model.Outsider;

public class ItemController 
{
	private static ItemController instance = null;

	public static ItemController getInstance()
	{
		if(instance == null)
			instance = new ItemController();

		return instance;
	}

	public ItemController()
	{

	}

	public int createItem(String name, double price, int stock, String dimensions, ArrayList<ItemSupplier> newItemOutsiders, ItemType type) throws SQLException
	{
		int result = -1;

		try
		{
			Item newItem = new Item();

			newItem.setName(name);
			newItem.setPrice(price);
			newItem.setStock(stock);
			newItem.setDimensions(dimensions);
			newItem.setSuppliers(newItemOutsiders);
			newItem.setType(type);

			DBConnection.startTransaction();

			IFItem dbItem = new DBItem();
			result = dbItem.createItem(newItem);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateItem(int itemID, String name, double price, int stock, String dimensions, ItemType type) throws SQLException
	{
		int result = -1;

		try
		{
			Item newItem = new Item();

			newItem.setItemID(itemID);
			newItem.setName(name);
			newItem.setPrice(price);
			newItem.setStock(stock);
			newItem.setDimensions(dimensions);
			newItem.setType(type);

			DBConnection.startTransaction();

			IFItem dbItem = new DBItem();
			result = dbItem.updateItem(newItem);

			DBConnection.commitTransaction();
		}
		catch (SQLException e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateItemQuantity(int itemID, int addStock, int removeStock) throws SQLException
	{
		int result = -1;

		try
		{
			DBConnection.startTransaction();

			IFItem dbItem = new DBItem();
			result = dbItem.updateItemQuantity(itemID, addStock, removeStock);

			DBConnection.commitTransaction();
		}
		catch (SQLException e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}


	public int addItemStock(int itemID, int quantity, Outsider supplier, boolean update) throws SQLException
	{
		int result = -1;

		try
		{
			DBConnection.startTransaction();

			IFItem dbItem = new DBItem();
			result = dbItem.addItemStock(itemID, quantity, supplier, update);

			DBConnection.commitTransaction();
		}
		catch (SQLException e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int getItemStock(int itemID) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.getItemStock(itemID);
	}

	public Item searchItemByID(int id) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.searchItemByID(id);
	}


	public Item searchItemByName(String name) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.searchItemByName(name);
	}

	public LinkedList<Item> printAllItemsWithStock() throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.printAllItemsWithStock();
	}

	public LinkedList<Item> searchItemEverything(boolean active, String type, String search) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.searchItemEverything(active, type, search);
	}

	public LinkedList<Item> searchItemName(boolean active, String type,	String search) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.searchItemName(active, type, search);
	}

	public LinkedList<Item> searchItemSupplier(boolean active, String type, String search) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.searchItemSupplier(active, type, search);
	}

	public LinkedList<Item> searchItemCity(boolean active, String type, String search) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.searchItemCity(active, type, search);
	}

	public LinkedList<Item> searchItemCountry(boolean active, String type, String search) throws SQLException
	{
		IFItem dbItem = new DBItem();

		return dbItem.searchItemCountry(active, type, search);
	}

	/* */

	public ArrayList<ItemSupplier> getItemSuppliers(int itemID, boolean active) throws SQLException
	{
		IFItemSupplier dbItemSupplier = new DBItemSupplier();

		return dbItemSupplier.getItemSuppliers(itemID, active);
	}

	public int updateItemSupplierStatus(int itemID, int supplierID) throws SQLException
	{
		int result = -1;

		try
		{
			DBConnection.startTransaction();

			IFItemSupplier dbItemSupplier = new DBItemSupplier();
			result = dbItemSupplier.updateItemSupplierStatus(itemID, supplierID);

			DBConnection.commitTransaction();
		}
		catch (SQLException e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	/*
	 *
	 *	Item Type
	 *
	 */

	public int createItemType(ItemTypeMeasurement typeMeasurement, String typeName) throws SQLException
	{
		int result = -1;

		try 
		{
			ItemType newItemType = new ItemType();

			newItemType.setTypeMeasurement(typeMeasurement);
			newItemType.setTypeName(typeName);

			DBConnection.startTransaction();

			IFItemType dbItemType = new DBItemType();
			result = dbItemType.createItemType(newItemType);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateItemType(int typeID, ItemTypeMeasurement typeMeasurement, String typeName) throws SQLException
	{
		int result = -1;

		try 
		{
			ItemType newItemType = new ItemType();

			newItemType.setTypeID(typeID);
			newItemType.setTypeMeasurement(typeMeasurement);
			newItemType.setTypeName(typeName);

			DBConnection.startTransaction();

			IFItemType dbItemType = new DBItemType();
			result = dbItemType.updateItemType(newItemType);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public ItemType findItemTypeByName(String name) throws SQLException
	{
		IFItemType dbItemType = new DBItemType();

		return dbItemType.findItemTypeByName(name);
	}

	public ArrayList<ItemType> printAllItemTypes() throws SQLException
	{
		IFItemType dbItemType = new DBItemType();

		return dbItemType.printAllItemTypes();
	}

	/*
	 *
	 *	Item Type Measurement
	 *
	 */

	public int createItemTypeMeasurement(String measurementName) throws SQLException
	{
		int result = -1;

		try 
		{
			ItemTypeMeasurement newItemTypeMeasurement = new ItemTypeMeasurement();

			newItemTypeMeasurement.setMeasurement(measurementName);

			DBConnection.startTransaction();

			IFItemTypeMeasurement dbItemTypeMeasurement = new DBItemTypeMeasurement();
			result = dbItemTypeMeasurement.createItemTypeMeasurement(newItemTypeMeasurement);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateItemTypeMeasurement(int measurementID, String measurementName) throws SQLException
	{
		int result = -1;

		try 
		{
			ItemTypeMeasurement newItemTypeMeasurement = new ItemTypeMeasurement();

			newItemTypeMeasurement.setID(measurementID);
			newItemTypeMeasurement.setMeasurement(measurementName);

			DBConnection.startTransaction();

			IFItemTypeMeasurement dbItemTypeMeasurement = new DBItemTypeMeasurement();
			result = dbItemTypeMeasurement.updateItemTypeMeasurement(newItemTypeMeasurement);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public ItemTypeMeasurement findItemTypeMeasurementByName(String typeMeasurement) throws SQLException
	{
		IFItemTypeMeasurement dbItemTypeMeasurement = new DBItemTypeMeasurement();

		return dbItemTypeMeasurement.findItemTypeMeasurementByName(typeMeasurement);
	}

	public ArrayList<ItemTypeMeasurement> printAllItemTypeMeasurement() throws SQLException
	{
		IFItemTypeMeasurement dbItemTypeMeasurement = new DBItemTypeMeasurement();

		return dbItemTypeMeasurement.printAllItemTypeMeasurements();
	}
}